package org.sanm.domain.orderCount;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.sanm.common.Result;
import org.sanm.domain.category.Category;
import org.sanm.domain.goods.Goods;
import org.sanm.domain.order.model.Order;
import org.sanm.domain.orderCount.entity.CountOrderByCategory;
import org.sanm.domain.orderCount.entity.CountOrderByCompany;
import org.sanm.domain.orderCount.entity.CountOrderByProject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/count")
@Tag(name = "订单统计", description = "订单统计")
@RequiredArgsConstructor
public class OrderCountController {

    private final OrderCountMapper orderCountMapper;

    @GetMapping("order/category")
    @Operation(summary = "根据商品类型统计", description = "根据商品类型统计")
    public Result<List<CountOrderByCategory>> countByCategory(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate
    ){

        List<CountOrderByCategory> list = orderCountMapper.countByCategory(startDate, endDate);
        return Result.success(list);
    }

    @GetMapping("order/company")
    @Operation(summary = "根据供应商统计", description = "根据供应商统计")
    public Result<List<CountOrderByCompany>> countByCompany(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate
    ){

        List<CountOrderByCompany> list = orderCountMapper.countByCompany(startDate, endDate);
        return Result.success(list);
    }

    @GetMapping("order/project")
    @Operation(summary = "根据项目统计", description = "根据项目统计")
    public Result<List<CountOrderByProject>> countByProject(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate
    ){

        List<CountOrderByProject> list = orderCountMapper.countByProject(startDate, endDate);
        return Result.success(list);
    }

    @GetMapping("order")
    @Operation(summary = "统计订单量", description = "统计订单量")
    public Result<List<Order>> countOrder(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate
    ){
        List<Order> list = Db.lambdaQuery(Order.class)
                .select(Order::getId, Order::getAmount)
                .between(startDate != null, Order::getCreateTime, startDate, endDate)
                .list();

        return Result.success(list);
    }

    @GetMapping("goods")
    @Operation(summary = "统计商品", description = "统计商品")
    public Result<Integer> countCategory(
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate
    ){
        List<Goods> list = Db.lambdaQuery(Goods.class)
                .select(Goods::getId)
                .list();
        return Result.success(list.size());
    }

}
